package com.sys.app.web.exceptions;

import com.sys.app.entity.result.ResultDTO;
import com.sys.app.entity.sys.CustomExceptions;
import com.sys.app.entity.sys.CustomPageExceptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * 全局异常处理返回
 */
@ControllerAdvice
public class CustomControllerAdvice {
    private static Logger logger = LoggerFactory.getLogger(CustomControllerAdvice.class);
    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView errorHandler(Exception ex) {
        logger.error(ex.toString());
        ModelAndView modelAndView = new ModelAndView("error/500");
        modelAndView.addObject("msg", ex.getMessage());
        return modelAndView;
    }

    /**
     * 拦截捕捉自定义异常 CustomExceptions.class
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = CustomExceptions.class)
    public ResultDTO CustomExceptionsErrorHandler(CustomExceptions ex) {
        logger.error(ex.getMsg());
        return ResultDTO.error().result(ex.getMsg());
    }

    /**
     * 拦截捕捉自定义异常 CustomExceptions.class
     * @param ex
     * @return
     */
    @ExceptionHandler(value = CustomPageExceptions.class)
    public ModelAndView CustomPageExceptionsErrorHandler(CustomPageExceptions ex) {
        logger.error(ex.getMsg());
        ModelAndView modelAndView = new ModelAndView("error/500");
        modelAndView.addObject("msg", ex.getMsg());
        return modelAndView;


    }
}
